sap.ui.define([
    "sap/ui/core/Control",
    "sap/m/Button",
    "sap/m/List",
    "sap/m/StandardListItem",
], function (
    Control,
    Button,
    List,
    StandardListItem,
) {
    "use strict";

    return Control.extend("autofiror.control.ButtonControl", {
        metadata: {
            properties: {
                buttonValue: { type: "string", defaultValue: "" },
                transmitValue: { type: "string", defaultValue: "" },
                listObject: { type: "object", defaultValue: null }
            },
            aggregations: {
                _button: { type: "sap.m.Button", multiple: false, visibility: "hidden" },
                _list: { type: "sap.m.List", multiple: false, visibility: "hidden" }
            },
            events: {
                pressButton: {
                    parameters: {}
                },
                pressButtonWithPar: {
                    parameters: {
                        value1: { type: "int" },
                        value2: { type: "int" }
                    }
                }
            }
        },

        onBeforeRendering() {
            // 在init中初始化的话会找不到传进来的值
            this.setAggregation("_button", new Button({
                text: this.getButtonValue(),
                press: this._onPressButton.bind(this)
            }).addStyleClass("sapUiTinyMarginTopBottom"));

            let oList = [];
            this.getListObject().forEach(item => {
                oList.push(new StandardListItem({
                    title: item.name
                }));
            });
            this.setAggregation("_list", new List({
                items: oList
            }).addStyleClass("sapUiTinyMarginTopBottom"));
        },

        _onPressButton() {
            this.fireEvent("pressButton");
            this.fireEvent("pressButtonWithPar", {
                value1: 123,
                value2: 234
            });
            console.log(this.getButtonValue());
            console.log(this.getTransmitValue());
            console.log(this.getListObject());
        },

        renderer(oRm, oControl) {
            // 此处的参数onControl指的就是该Control组件
            oRm.openStart("div", oControl);
            // 创建一个div
            oRm.openEnd();
            oRm.renderControl(oControl.getAggregation("_button"));
            oRm.renderControl(oControl.getAggregation("_list"));
            oRm.renderControl(oControl.getAggregation("_card"));
            oRm.close("div");
            // 闭合div
        }
    });
});